﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory;
using com.iss.inventory.CustomModels;
using System.Web.Security;

namespace Staionery_Inventory
{
    public partial class login : System.Web.UI.Page
    {
        MainController mc;

      
        protected void Page_Load(object sender, EventArgs e)
        {
            mc = new MainController();
         }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            List<string> log = new List<string>();
           
            if (btnLogin.Text == "Login")
            {
                String empID = txtEmployeeID.Text.ToString();
                String password = txtPass.Text.ToString();
                log = mc.LoginController(empID, password);
                if (log.Count < 2)
                {
                    lblLoginInfo.Text = log[0];
                    txtEmployeeID.Text = "";
                }
                else
                {
                    Session["empID"] = int.Parse(txtEmployeeID.Text);
                    Session["UserName"] = log[0];
                    Session["role"] = log[1];
                    Session["Department"] = log[2];
                    Session["ApproveID"] = log[3];
                    
                    if (log.Count>4)
                    {
                        btndelegate.Visible = true;
                        btndelegate.Text = log[5].ToString();
                        btnLogin.Text = log[0].ToString();
                        Session["DelUserName"] = log[5];
                        Session["Delrole"] = log[4];
                        Session["DeleEmployeeID"] = log[6];
                        txtEmployeeID.Text = null;

                        txtEmployeeID.Enabled = false;
                        txtPass.Enabled = false;
                    }
                    else
                    {
                        if (log[1] == "Head")
                        {
                            Response.Redirect("~/Presentation/StationeryRequest/StationeryRequestList.aspx");
                        }
                        else if (log[1] == "Admin")
                        {
                            Response.Redirect("~/Presentation/Admin/UserMaintenanceUI.aspx");
                        }
                        else
                        {
                            Response.Redirect("~/Presentation/Search/SearchItemsPage.aspx");
                        }
                    }


                  
                }

            }
                 else
    {
        Button btn = sender as Button;
        if (btn.ID == "btnLogin")
        {
            Session["empID"] = Session["empID"];
            if ( "Head"==(string)Session["role"])
            {
                Response.Redirect("~/Presentation/StationeryRequest/StationeryRequestList.aspx");
            }
            else
                Response.Redirect("~/Presentation/Search/SearchItemsPage.aspx");
        }
        else
        {
            Session["empID"] = Session["DeleEmployeeID"];
            Session["UserName"] =  Session["DelUserName"];
            Session["DelUserName"] = null;
            Session[""] = Session[""];
            Session["role"] = Session["Delrole"];
            Session["Delrole"] = null;
            if ((string)Session["role"] == "Head")
            {

                Response.Redirect("~/Presentation/StationeryRequest/StationeryRequestList.aspx");
            }
            else
                Response.Redirect("~/Presentation/Search/SearchItemsPage.aspx");


        }
    }
        }
   
      
    }
}